package Model;

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

public class Chessboard {
	private ArrayList<Observer> obs;
	private Piece [][] board;
	
	public Piece[][] getBoard() {
		return board;
	}

	public void setBoard(Piece[][] board) {
		this.board = board;
	}

	public Chessboard(){
		obs = new ArrayList<Observer>();
		board = new Piece[8][8];
	}
	
	public void addPieces(List<Piece> list){
		for(Piece element : list){
			board[element.getX()][element.getY()] = element;
		}
	}
	
	public void movePiece(Point p1, Point p2)
	{
		if(getPiece(p2) != null)
		{
			getPiece(p2).setPoint(new Point (-1,-1));
		}
		getPiece(p1).setPoint(p2);
		getPiece(p1).incNbTurns();
		board[p2.x][p2.y] = board[p1.x][p1.y];
		board[p1.x][p1.y] = null;
	}
	
	public Piece getPiece(Point p)
	{
		if (Piece.checkCoord(p.x) && Piece.checkCoord(p.y))
			return board[p.x][p.y];
		return null;
	}
	
	public void clear()
	{
		for(int y=0 ; y<8 ; y++)
		{
			for(int x=0 ; x<8 ; x++)
			{
				board[x][y] = null;
			}
		}
	}
	
	public void printBoard()
	{
		System.out.println("  |0 |1 |2 |3 |4 |5 |6 |7 |");
		System.out.println("---------------------------");
		for(int y=0 ; y<8 ; y++)
		{
			System.out.print(y+" |");
			for(int x=0 ; x<8 ; x++)
			{
				if(board[x][y] == null)
					System.out.print("  |");
				else
				{
					if(board[x][y].getColor() == Color.White)
						System.out.print("w");
					else
						System.out.print(" ");
					System.out.print(board[x][y].getChar() + "|");
				}
			}
			System.out.println();
		}
	}
}
